﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using CRMBiz;
using CRMWeb.Utilities.Context;
using System.Web.Configuration;

namespace CRMWeb.Pages.AM.EzidebitService
{
    public partial class AddCustomer : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                EziDebitService ezidebitservice = new EziDebitService();
                XmlDocument response = ezidebitservice.addCustomer(WebConfigurationManager.AppSettings["EziDebitDigitalKey"], txtYourSystemReference.Text, txtYourGeneralReference.Text, txtLastName.Text, txtFirstName.Text, txtAddressLine1.Text, txtAddressLine2.Text, txtAddressSuburb.Text, txtAddressState.Text, txtAddressPostcode.Text, txtEmailAddress.Text, txtMobilePhoneNumber.Text, txtContractStartDate.Text, txtSmsPaymentReminder.Text, txtSmsFailedNotification.Text, txtSmsExpiredCard.Text, txtUsername.Text);
                DataSet ds = ConvertXMLtoDataSet(response.InnerXml);
                if (ds.Tables["Data"].Columns.Contains("Error") && ds.Tables["Data"].Rows.Count > 0)
                    lblMsg.Text = ds.Tables["Data"].Rows[0]["ErrorMessage"].ToString();
                else
                    lblMsg.Text = "Customer Added Successfully and Your customer reference is:" + ds.Tables["Data"].Rows[0]["CustomerRef"].ToString(); 
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex, CMSContext.CurrentUser.MemberName);
            }
        }

        private DataSet ConvertXMLtoDataSet(string response)
        {
            DataSet ds = new DataSet();
            try
            {
                using (StringReader stringReader = new StringReader(response))
                {
                    ds = new DataSet();
                    ds.ReadXml(stringReader);
                }
            }
            catch (Exception ex)
            {
                ErrorHandler.saveErrorLog(ex, CMSContext.CurrentUser.MemberName);
            }
            return ds;
        }
    }
}